package service

import (
	"mariadb-operator/pkg/apis/finupgroup/v1alpha1"
	"mariadb-operator/pkg/constants"

	"k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// New will return a new kubernetes Service by MariaDBCluster crd
func New(cluster *v1alpha1.MariaDBCluster, name string, headless bool) *v1.Service {
	svc := &v1.Service{
		TypeMeta: metav1.TypeMeta{
			Kind:       "Service",
			APIVersion: "v1",
		},
		ObjectMeta: metav1.ObjectMeta{
			Labels:    map[string]string{constants.ClusterLabel: cluster.Name},
			Name:      name,
			Namespace: cluster.Namespace,
		},
		Spec: v1.ServiceSpec{
			Ports: []v1.ServicePort{
				v1.ServicePort{
					Name: constants.MariaDBContainerPortName,
					Port: constants.MariaDBContainerPort,
				},
			},
			Selector: map[string]string{
				constants.ClusterLabel: cluster.Name,
			},
		},
	}
	if headless {
		svc.Spec.ClusterIP = v1.ClusterIPNone
	}
	return svc
}
